import { createRouter, createWebHistory } from 'vue-router'


import HomeView from '../views/HomeView.vue'
import testView from '../views/testView.vue'
import articleShowView from '../views/article/articleShowView.vue'
import articleUploadView from '../views/article/articleUploadView.vue'

import newArticleView from '../views/tag/newArticleView.vue';
import activityView from '../views/tag/activityView.vue';
import cultureView from '../views/tag/cultureView.vue';
import personView from '../views/tag/personView.vue';
import sightView from '../views/tag/sightView.vue';
import historyView from '../views/tag/historyView.vue';

import adminLoginView from '../views/background/adminLoginView.vue';
import adminHomeView from '../views/background/adminHomeView.vue';
import adminManageView from '../views/background/adminManageView.vue';
import checkHistoryView from '../views/background/checkHistoryView.vue';
import checkArticleView from '../views/background/checkArticleView.vue';
import homeSettingView from '../views/background/homeSettingView.vue';
import adminMyArticleView from '../views/background/adminMyArticleView.vue';
import adminUnCheckView from '../views/background/adminUnCheckView.vue';
import notfoundView from '../views/notFoundView.vue';

import searchArticleView from '../views/tag/searchArticleView.vue';

import store from '@/store'
const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      needNav: true,
      title: "江西省红色文化资源管理系统",
    },
  },
  {
    path: '/test/',
    name: 'test',
    component: testView,
    meta: {
      needNav: true,

    },
  },
  {
    path: '/article/',
    name: 'article',
    component: articleShowView,
    meta: {
      needNav: true,
      title: " ",
    },
  },
  {
    path: '/login/',
    name: 'login',
    component: adminLoginView,
    meta: {
      needNav: false,
      title: "后台登录",
    },
  },
  {
    path: '/article/upload/',
    name: 'articleUpload',
    component: articleUploadView,
    meta: {
      needNav: false,
      admin: true,
      needAdminNav: true,
      title: "文章上传",
    },
  },
  {
    path: '/tag/activity/',
    name: 'activity',
    component: activityView,
    meta: {
      needNav: true,
      title: "精彩活动",
    },
  },
  {
    path: '/tag/culture/',
    name: 'culture',
    component: cultureView,
    meta: {
      needNav: true,
      title: "红色文化",
    },
  },
  {
    path: '/tag/history/',
    name: 'history',
    component: historyView,
    meta: {
      needNav: true,
      title: "红色历史",
    },
  },
  {
    path: '/tag/newArticle/',
    name: 'newArticle',
    component: newArticleView,
    meta: {
      needNav: true,
      title: "新闻资讯",
    },
  },
  {
    path: '/tag/person/',
    name: 'person',
    component: personView,
    meta: {
      needNav: true,
      title: "人物故事",
    },
  },
  {
    path: '/tag/sight/',
    name: 'sight',
    component: sightView,
    meta: {
      needNav: true,
      title: "红色景点",
    },
  },
  {
    path: '/searchArticle/',
    name: 'searchArticle',
    component: searchArticleView,
    meta: {
      needNav: true,
      title: "搜索文章",
    },
  },
  {
    path: '/adminHome/',
    name: 'adminHome',
    component: adminHomeView,
    meta: {
      needNav: false,
      admin: true,
      needAdminNav: true,
      superAdmin: true,
    },
  },
  {
    path: '/adminManage/',
    name: 'adminManage',
    component: adminManageView,
    meta: {
      needNav: false,
      admin: true,
      needAdminNav: true,
      superAdmin: true,
      title: "管理员管理",
    },
  },
  {
    path: '/checkArticle/',
    name: 'checkArticle',
    component: checkArticleView,
    meta: {
      needNav: false,
      admin: true,
      needAdminNav: true,
      superAdmin: true,
      title: "文章审核",
    },
  },
  {
    path: '/checkHistory/',
    name: 'checkHistory',
    component: checkHistoryView,
    meta: {
      needNav: false,
      admin: true,
      needAdminNav: true,
      superAdmin: true,
      title: "审核历史",
    },
  },
  {
    path: '/homeSetting/',
    name: 'homeSetting',
    component: homeSettingView,
    meta: {
      needNav: false,
      admin: true,
      needAdminNav: true,
      superAdmin: true,
      title: "首页管理",
    },
  },
  {
    path: '/myArticle/',
    name: 'adminMyArticle',
    component: adminMyArticleView,
    meta: {
      needNav: false,
      admin: true,
      needAdminNav: true,
      title: "我的已审核文章",
    },
  },
  {
    path: '/adminUnCheck/',
    name: 'adminUnCheck',
    component: adminUnCheckView,
    meta: {
      needNav: false,
      admin: true,
      needAdminNav: true,
      title: "我的未审核文章",
    },
  },
  {
    path: '/notFound/',
    name: 'notFound',
    component: notfoundView,
    meta: {
      needNav: true,
      admin: false,
      needAdminNav: false,
      title: "404",
    },
  },
  {
    path: '/:pathMatch(.*)',
    redirect: "/notfound/",
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes,
})
export default router

router.beforeEach((to, from, next) => {
  window.scroll(0, 0);
  if (to.meta.title)
    (document.title = to.meta.title);
  if (to.meta.admin && !store.state.admin.isLogin) {//需要验证并且没有登录
    next({ name: "login" });
  } else {
    if (to.meta.superAdmin && store.state.admin.role == '管理员')
      next({ name: "adminMyArticle" });
    else next();
  }
})